package com.service.impl;

import com.mapper.TagsMapper;
import com.pojo.Result;
import com.pojo.Tags;
import com.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TagsServiceImpl implements TagsService {

    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public Result<Void> createTag(Tags tag) {
        try {
            // 设置创建时间为当前时间
            tag.setCreatedAt(LocalDateTime.now());
            int result = tagsMapper.insert(tag);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("创建标签失败");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> updateTag(Tags tag) {
        try {
            int result = tagsMapper.update(tag);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新标签失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> deleteTag(Integer id) {
        try {
            int result = tagsMapper.delete(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除标签失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Tags> getTagById(Integer id) {
        try {
            Tags tag = tagsMapper.selectById(id);
            if (tag != null) {
                return Result.success(tag);
            } else {
                return Result.error("标签不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Tags>> getAllTags() {
        try {
            List<Tags> tags = tagsMapper.selectAll();
            return Result.success(tags);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}